home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 359_11 / patch5.000 / GO32_UNASSMBL.C < prev    next >
C/C++ Source or Header  |  1991-09-11  |  28KB  |  1,134 lines

  1. /* This is file UNASSMBL.C */
  2. /*
  3. ** Copyright (C) 1991 DJ Delorie, 24 Kirsten Ave, Rochester NH 03867-2954
  4. **
  5. ** This file is distributed under the terms listed in the document
  6. ** "copying.dj", available from DJ Delorie at the address above.
  7. ** A copy of "copying.dj" should accompany this file; if not, a copy
  8. ** should be available from where this file was obtained.  This file
  9. ** may not be distributed without a verbatim copy of "copying.dj".
  10. **
  11. ** This file is distributed WITHOUT ANY WARRANTY; without even the implied
  12. ** warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. */
  14.  
  15. /* History:255,3 */
  16. #include <stdio.h>
  17. #include <string.h>
  18.  
  19. #include "build.h"
  20. #include "types.h"
  21. #include "gdt.h"
  22. #include "idt.h"
  23. #include "tss.h"
  24. #include "utils.h"
  25. #include "unassmbl.h"
  26. #include "syms.h"
  27. #include "mono.h"
  28.  
  29. #if DEBUGGER
  30.  
  31. int seg_size=32;
  32.  
  33. static word8 buf[20];
  34. static word32 vaddr;
  35. static int bufp, bufe;
  36. static char ubuf[100], *ubufp;
  37. static col;
  38.  
  39. /* Percent tokens in strings:
  40.    First char after '%':
  41.     A - direct address
  42.     C - reg of r/m picks control register
  43.     D - reg of r/m picks debug register
  44.     E - r/m picks operand
  45.     F - flags register
  46.     G - reg of r/m picks general register
  47.     I - immediate data
  48.     J - relative IP offset
  49.     M - r/m picks memory
  50.     O - no r/m, offset only
  51.     R - mod of r/m picks register only
  52.     S - reg of r/m picks segment register
  53.     T - reg of r/m picks test register
  54.     X - DS:ESI
  55.     Y - ES:EDI
  56.     2 - prefix of two-byte opcode
  57.     e - put in 'e' if use32 (second char is part of reg name)
  58.         put in 'w' for use16 or 'd' for use32 (second char is 'w')
  59.     f - floating point (second char is esc value)
  60.     g - do r/m group 'n'
  61.     p - prefix
  62.     s - size override (second char is a,o)
  63.    Second char after '%':
  64.     a - two words in memory (BOUND)
  65.     b - byte
  66.     c - byte or word
  67.     d - dword
  68.     p - 32 or 48 bit pointer
  69.     s - six byte pseudo-descriptor
  70.     v - word or dword
  71.     w - word
  72.     F - use floating regs in mod/rm
  73.     1-8 - group number, esc value, etc
  74. */
  75.  
  76. char *opmap1[] = {
  77. /* 0 */
  78.   "add %Eb,%Gb", "add %Ev,%Gv", "add %Gb,%Eb", "add %Gv,%Ev",
  79.   "add al,%Ib", "add %eax,%Iv", "push es", "pop es",
  80.   "or %Eb,%Gb", "or %Ev,%Gv", "or %Gb,%Eb", "or %Gv,%Ev",
  81.   "or al,%Ib", "or %eax,%Iv", "push cs", "%2 ",
  82. /* 1 */
  83.   "adc %Eb,%Gb", "adc %Ev,%Gv", "adc %Gb,%Eb", "adc %Gv,%Ev",
  84.   "adc al,%Ib", "adc %eax,%Iv", "push ss", "pop ss",
  85.   "sbb %Eb,%Gb", "sbb %Ev,%Gv", "sbb %Gb,%Eb", "sbb %Gv,%Ev",
  86.   "sbb al,%Ib", "sbb %eax,%Iv", "push ds", "pop ds",
  87. /* 2 */
  88.   "and %Eb,%Gb", "and %Ev,%Gv", "and %Gb,%Eb", "and %Gv,%Ev",
  89.   "and al,%Ib", "and %eax,%Iv", "%pe", "daa",
  90.   "sub %Eb,%Gb", "sub %Ev,%Gv", "sub %Gb,%Eb", "sub %Gv,%Ev",
  91.   "sub al,%Ib", "sub %eax,%Iv", "%pc", "das",
  92. /* 3 */
  93.   "xor %Eb,%Gb", "xor %Ev,%Gv", "xor %Gb,%Eb", "xor %Gv,%Ev",
  94.   "xor al,%Ib", "xor %eax,%Iv", "%ps", "aaa",
  95.   "cmp %Eb,%Gb", "cmp %Ev,%Gv", "cmp %Gb,%Eb", "cmp %Gv,%Ev",
  96.   "cmp al,%Ib", "cmp %eax,%Iv", "%pd", "aas",
  97. /* 4 */
  98.   "inc %eax", "inc %ecx", "inc %edx", "inc %ebx",
  99.   "inc %esp", "inc %ebp", "inc %esi", "inc %edi",
  100.   "dec %eax", "dec %ecx", "dec %edx", "dec %ebx",
  101.   "dec %esp", "dec %ebp", "dec %esi", "dec %edi",
  102. /* 5 */
  103.   "push %eax", "push %ecx", "push %edx", "push %ebx",
  104.   "push %esp", "push %ebp", "push %esi", "push %edi",
  105.   "pop %eax", "pop %ecx", "pop %edx", "pop %ebx",
  106.   "pop %esp", "pop %ebp", "pop %esi", "pop %edi",
  107. /* 6 */
  108.   "pusha", "popa", "bound %Gv,%Ma", "arpl %Ew,%Rw",
  109.   "%pf", "%pg", "%so", "%sa",
  110.   "push %Iv", "imul %Gv=%Ev*%Iv", "push %Ib", "imul %Gv=%Ev*%Ib",
  111.   "insb %Yb,dx", "ins%ew %Yv,dx", "outsb dx,%Xb", "outs%ew dx,%Xv",
  112. /* 7 */
  113.   "jo %Jb", "jno %Jb", "jnc %Jb", "jc %Jb",
  114.   "jz %Jb", "jnz %Jb", "jbe %Jb", "jnbe %Jb",
  115.   "js %Jb", "jns %Jb", "jpe %Jb", "jpo %Jb",
  116.   "jl %Jb", "jge %Jb", "jle %Jb", "jg %Jb",
  117. /* 8 */
  118.   "%g1 %Eb,%Ib", "%g1 %Ev,%Iv", "mov al,%Ib", "%g1 %Ev,%Ib",
  119.   "test %Eb,%Gb", "test %Ev,%Gv", "xchg %Eb,%Gb", "xchg %Ev,%Gv",
  120.   "mov %Eb,%Gb", "mov %Ev,%Gv", "mov %Gb,%Eb", "mov %Gv,%Ev",
  121.   "mov %Ew,%Sw", "lea %Gv,%M ", "mov %Sw,%Ew", "pop %Ev",
  122. /* 9 */
  123.   "nop", "xchg %eax,%ecx", "xchg %eax,%edx", "xchg %eax,%ebx",
  124.   "xchg %eax,%esp", "xchg %eax,%ebp", "xchg %eax,%esi", "xchg %eax,%edi",
  125.   "cbw", "cwd", "call %Ap", "fwait",
  126.   "push %eflags", "pop %eflags", "sahf", "lahf",
  127. /* a */
  128.   "mov al,%Ob", "mov %eax,%Ov", "mov %Ob,al", "mov %Ov,%eax",
  129.   "movsb %Xb,%Yb", "movs%ew %Xv,%Yv", "cmpsb %Xb,%Yb", "cmps%ew %Xv,%Yv",
  130.   "test al,%Ib", "test %eax,%Iv", "stosb %Yb,al", "stos%ew %Yv,%eax",
  131.   "lodsb al,%Xb", "lods%ew %eax,%Xv", "scasb al,%Xb", "scas%ew %eax,%Xv",
  132. /* b */
  133.   "mov al,%Ib", "mov cl,%Ib", "mov dl,%Ib", "mov bl,%Ib",
  134.   "mov ah,%Ib", "mov ch,%Ib", "mov dh,%Ib", "mov bh,%Ib",
  135.   "mov %eax,%Iv", "mov %ecx,%Iv", "mov %edx,%Iv", "mov %ebx,%Iv",
  136.   "mov %esp,%Iv", "mov %ebp,%Iv", "mov %esi,%Iv", "mov %edi,%Iv",
  137. /* c */
  138.   "%g2 %Eb,%Ib", "%g2 %Ev,%Ib", "ret %Iw", "ret",
  139.   "les %Gv,%Mp", "lds %Gv,%Mp", "mov %Eb,%Ib", "mov %Ev,%Iv",
  140.   "enter %Iw,%Ib", "leave", "retf %Iw", "retf",
  141.   "int 3", "int %Ib", "into", "iret",
  142. /* d */
  143.   "%g2 %Eb,1", "%g2 %Ev,1", "%g2 %Eb,cl", "%g2 %Ev,cl",
  144.   "aam", "aad", 0, "xlat",
  145. #if 0
  146.   "esc 0,%Ib", "esc 1,%Ib", "esc 2,%Ib", "esc 3,%Ib",
  147.   "esc 4,%Ib", "esc 5,%Ib", "esc 6,%Ib", "esc 7,%Ib",
  148. #else
  149.   "%f0", "%f1", "%f2", "%f3",
  150.   "%f4", "%f5", "%f6", "%f7",
  151. #endif
  152. /* e */
  153.   "loopne %Jb", "loope %Jb", "loop %Jb", "jcxz %Jb",
  154.   "in al,%Ib", "in %eax,%Ib", "out %Ib,al", "out %Ib,%eax",
  155.   "call %Jv", "jmp %Jv", "jmp %Ap", "jmp %Jb",
  156.   "in al,dx", "in %eax,dx", "out dx,al", "out dx,%eax",
  157. /* f */
  158.   "lock %p ", 0, "repne %p ", "rep(e) %p ",
  159.   "hlt", "cmc", "%g3", "%g0",
  160.   "clc", "stc", "cli", "sti",
  161.   "cld", "std", "%g4", "%g5"
  162.   };
  163.  
  164. char *second[] = {
  165. /* 0 */
  166.   "%g6", "%g7", "lar %Gv,%Ew", "lsl %Gv,%Ew", 0, 0, "clts", 0,
  167.   0, 0, 0, 0, 0, 0, 0, 0,
  168. /* 1 */
  169.   0, 0, 0, 0, 0, 0, 0, 0,
  170.   0, 0, 0, 0, 0, 0, 0, 0,
  171. /* 2 */
  172.   "mov %Rd,%Cd", "mov %Rd,%Dd", "mov %Cd,%Rd", "mov %Dd,%Rd",
  173.   "mov %Rd,%Td", 0, "mov %Td,%Rd", 0,
  174.   0, 0, 0, 0, 0, 0, 0, 0,
  175. /* 3 */
  176.   0, 0, 0, 0, 0, 0, 0, 0,
  177.   0, 0, 0, 0, 0, 0, 0, 0,
  178.   0, 0, 0, 0, 0, 0, 0, 0,
  179.   0, 0, 0, 0, 0, 0, 0, 0,
  180.   0, 0, 0, 0, 0, 0, 0, 0,
  181.   0, 0, 0, 0, 0, 0, 0, 0,
  182.   0, 0, 0, 0, 0, 0, 0, 0,
  183.   0, 0, 0, 0, 0, 0, 0, 0,
  184.   0, 0, 0, 0, 0, 0, 0, 0,
  185.   0, 0, 0, 0, 0, 0, 0, 0,
  186. /* 8 */
  187.   "jo %Jv", "jno %Jv", "jnc %Jv", "jc %Jv",
  188.   "jz %Jv", "jnz %Jv", "jbe %Jv", "jnbe %Jv",
  189.   "js %Jv", "jns %Jv", "jpe %Jv", "jpo %Jv",
  190.   "jl %Jv", "jge %Jv", "jle %Jv", "jg %Jv",
  191. /* 9 */
  192.   "seto %Eb", "setno %Eb", "setnc %Eb", "setc %Eb",
  193.   "setz %Eb", "setnz %Eb", "setbe %Eb", "setnbe %Eb",
  194.   "sets %Eb", "setns %Eb", "setp %Eb", "setnp %Eb",
  195.   "setl %Eb", "setge %Eb", "setle %Eb", "setg %Eb",
  196. /* a */
  197.   "push fs", "pop fs", 0, "bt %Ev,%Gv",
  198.   "shld %Ev,%Gv,%Ib", "shld %Ev,%Gv,cl", 0, 0,
  199.   "push gs", "pop gs", 0, "bts %Ev,%Gv",
  200.   "shrd %Ev,%Gv,%Ib", "shrd %Ev,%Gv,cl", 0, "imul %Gv,%Ev",
  201. /* b */
  202.   0, 0, "lss %Mp", "btr %Ev,%Gv",
  203.   "lfs %Mp", "lgs %Mp", "movzx %Gv,%Eb", "movzx %Gv,%Ew",
  204.   0, 0, "%g8 %Ev,%Ib", "btc %Ev,%Gv",
  205.   "bsf %Gv,%Ev", "bsr %Gv,%Ev", "movsx %Gv,%Eb", "movsx %Gv,%Ew",
  206. /* c */
  207.   0, 0, 0, 0, 0, 0, 0, 0,
  208.   0, 0, 0, 0, 0, 0, 0, 0,
  209.   0, 0, 0, 0, 0, 0, 0, 0,
  210.   0, 0, 0, 0, 0, 0, 0, 0,
  211.   0, 0, 0, 0, 0, 0, 0, 0,
  212.   0, 0, 0, 0, 0, 0, 0, 0,
  213.   0, 0, 0, 0, 0, 0, 0, 0,
  214.   0, 0, 0, 0, 0, 0, 0, 0,
  215.   };
  216.  
  217. char *groups[][8] = {   /* group 0 is group 3 for %Ev set */
  218.   { "test %Ev,%Iv", "test %Ev,%Iv,", "not %Ev", "neg %Ev",
  219.     "mul %eax,%Ev", "imul %eax,%Ev", "div %eax,%Ev", "idiv %eax,%Ev" },
  220.   { "add", "or", "adc", "sbb", "and", "sub", "xor", "cmp" },
  221.   { "rol", "ror", "rcl", "rcr", "shl", "shr", "shl", "sar" },
  222.   { "test %Eb,%Ib", "test %Eb,%Ib,", "not %Eb", "neg %Eb",
  223.     "mul al,%Eb", "imul al,%Eb", "div al,%Eb", "idiv al,%Eb" },
  224.   { "inc %Eb", "dec %Eb", 0, 0, 0, 0, 0, 0 },
  225.   { "inc %Ev", "dec %Ev", "call %Ev", "call %Ep",
  226.     "jmp %Ev", "jmp %Ep", "push %Ev", 0 },
  227.   { "sldt %Ew", "str %Ew", "lldt %Ew", "ltr %Ew",
  228.     "verr %Ew", "verw %Ew", 0, 0 },
  229.   { "sgdt %Ms", "sidt %Ms", "lgdt %Ms", "lidt %Ms",
  230.     "smsw %Ew", 0, "lmsw %Ew",